/*
 * Copyright 2016 jeasonlzy(廖子尧)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hisan.freeride.common.callback;


import android.app.Activity;

import com.hisan.freeride.common.base.BaseAppLication;
import com.hisan.freeride.common.utils.CollectionUtils;
import com.hisan.freeride.common.utils.utils.TokenUtils;
import com.hisan.freeride.common.view.SweetAlertDialog;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;

/**
 * ================================================
 * 作    者：jeasonlzy（廖子尧）Github地址：https://github.com/jeasonlzy
 * 版    本：1.0
 * 创建日期：2016/1/14
 * 描    述：对于网络请求是否需要弹出进度对话框
 * 修订历史：
 * ================================================
 */
public class NoDialogCallback<T> extends JsonCallback<T> {
    private  SweetAlertDialog dialog;

    public  SweetAlertDialog getInstance(Activity activity) {
        if (dialog == null) {
            synchronized (SweetAlertDialog.class) {
                dialog=new SweetAlertDialog(activity);
            }
        }
        return dialog;
    }

    public NoDialogCallback(Activity activity) {
        super();
        getInstance(activity);
    }

    @Override
    public void onStart(Request<T, ? extends Request> request) {

        HttpParams params=request.getParams();
        if (!CollectionUtils.isNullOrEmpty(params.urlParamsMap)){
            // com.blankj.utilcode.util.LogUtils.v("xxx",httpParams.toString());
//            Map<String, String> map = new HashMap<>();
//            for (Map.Entry<String, List<String>> entry : params.urlParamsMap.entrySet()) {
//                map.put(entry.getKey(), entry.getValue().get(0));
//            }
//          //  Map<String,String > maps= (Map<String, String>) GsonUtils.GsonToListMaps(httpParams.toString().trim());
//            String sign= BuildParamters.buildSign(map);
            request.getParams().put("city_id", BaseAppLication.getInstance().getCityid());
        }
        String token= TokenUtils.getToken();
        if (!CollectionUtils.isNullOrEmpty(token))
            request.getHeaders().put("token", token);
        request.getHeaders().put("client", "Android");
        request.getHeaders().put("version", "1.1.3");
    }

    @Override
    public void onFinish() {
        //网络请求结束后关闭对话框
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }

    @Override
    public void onError(Response<T> response) {
        super.onError(response);
        if (dialog.isShowing()) {
            dialog.dismiss();
        }
    }
}
